package net.codeyouth.codegen.buider

import net.codeyouth.codegen.bean.Config
import net.codeyouth.codegen.util.VelocityUtil
import org.apache.velocity.Template
import org.apache.velocity.VelocityContext
import org.slf4j.LoggerFactory

import java.time.Instant

/**
 * @author Frank create on 16/2/24 11:23
 */
class BaseServiceBuilder {

    private static final LOGGER = LoggerFactory.getLogger(BeanBuilder.class)

    static void build() {
        def engine = VelocityUtil.simpleEngine
        engine.init()

        Template template = engine.getTemplate("template/JavaBaseServiceTemplate.vm")

        VelocityContext context = new VelocityContext()

        context.put("basePackage", Config.CONFIG_BASE_PACKAGE)
        context.put("author", Config.CONFIG_AUTHOR)
        context.put("date", Instant.now().toString())

        String path = Config.CONFIG_SRC_PATH + "/" + Config.CONFIG_BASE_PACKAGE.replaceAll("\\.", "/") + "/service/BaseService.java"
        File folder = new File(path.substring(0, path.lastIndexOf("/")))
        if (!folder.exists() || !folder.isDirectory()) {
            folder.mkdirs()
        }
        FileWriter fw = new FileWriter(path)
        template.merge(context, fw)

        println(fw.toString())

        fw.flush()
        fw.close()
    }
}
